package net.cmkj.mine.common.taglib;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.beetl.core.Tag;

import com.jfinal.kit.StrKit;

import net.cmkj.mine.bean.SysUser;
import net.cmkj.mine.util.IWebUtils;

public class ButtonTagHtml extends Tag {
	
	@SuppressWarnings("unchecked")
	@Override
	public void render() {
		try {
			/**
			 * 需要的权限
			 */
			Map<String, String> attrs = (Map<String, String>) args[1];
			String id = attrs.get("id");
		    String permission = attrs.get("permission");
			String iconCls = attrs.get("iconCls");
			String size = attrs.get("size");
			String event = attrs.get("event");
			String onclick = attrs.get("onclick");
			String textName = attrs.get("textName");
			boolean hasPermission = true;
			StringBuilder button = new StringBuilder("<a href=\"javascript:void(0);\"");
			if (StrKit.notBlank(permission)) {
				HttpServletRequest request = (HttpServletRequest) ctx.getGlobal("request");
				SysUser sysUser = IWebUtils.getCurrentSysUser(request);
				if (sysUser == null || !sysUser.getPermissionSets().contains(permission)) {
					hasPermission = false;
				}
			}
			if (hasPermission) {
				if(StrKit.notBlank(id)){
					button.append(" id=\""+id+"\"");
				}
				button.append(" class=\"layui-btn ");
				if(StrKit.notBlank(iconCls)){
					button.append(" "+iconCls);
				}
				if(StrKit.notBlank(size)) {
					button.append(" "+size);
				}
				button.append("\"");
				if(StrKit.notBlank(event)){
					button.append(" lay-event=\""+event+"\"");
				}
				if(StrKit.notBlank(onclick)){
					button.append(" onclick=\""+onclick+"\"");
				}
				button.append(">");
				if(StrKit.notBlank(textName)){
					button.append(textName);
				}
				button.append("</a>");
				ctx.byteWriter.writeString(button.toString());
			} else {
				ctx.byteWriter.writeString("");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
